<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>AdminLTE 2 | Log in</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="/static/adminlte/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="/static/adminlte/bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="/static/adminlte/bower_components/Ionicons/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="/static/adminlte/css/AdminLTE.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="/static/adminlte/plugins/iCheck/square/blue.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
<!-- Google Font -->
<link href=<link href="https://fonts.useso.com/css?family=Source+Sans+Pro:300,400,600&subset=latin,latin-ext" rel="stylesheet"></head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="../../index2.html"><b>Admin</b>LTE</a>
  </div>
  <!-- /.login-logo -->
  <div class="login-box-body">
    <p class="login-box-msg">Sign in to start your session</p>

    <form action="../../index2.html" method="post" id="userLoginFrm">
      <div class="form-group has-feedback">
        <input id="username" name="username" type="input" class="form-control" placeholder="Email">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input id="password" name="password" type="password" class="form-control" placeholder="Password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="row">
        <div class="col-xs-8">
          <div class="checkbox icheck">
            <label>
              <input type="checkbox" id="rememberme"> Remember Me
            </label>
          </div>
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <button id="signin" type="button" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div>
        <!-- /.col -->
      </div>
    </form>
    <div id="errormsg" class="alert-danger text-center"></div>
    <div class="social-auth-links text-center">
      <p>- OR -</p>
      <a href="#" class="btn btn-block btn-social btn-facebook btn-flat"><i class="fa fa-facebook"></i> Sign in using
        Facebook</a>
      <a href="#" class="btn btn-block btn-social btn-google btn-flat"><i class="fa fa-google-plus"></i> Sign in using
        Google+</a>
    </div>
    <!-- /.social-auth-links -->

    <a href="#">I forgot my password</a><br>
    <a href="register.html" class="text-center">Register a new membership</a>

  </div>
  <!-- /.login-box-body -->
</div>
<!-- /.login-box -->

<!-- jQuery 3 -->
<script src="/static/adminlte/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/static/adminlte/bower_components/jquery/jquery.cookie.js"></script>
<script src="/static/adminlte/bower_components/jquery/jquery.md5.js"></script>

<!-- Bootstrap 3.3.7 -->
<script src="/static/adminlte/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="/static/adminlte/plugins/iCheck/icheck.min.js"></script>
<!-- jQuery Validation -->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
<script>
  $(function () {
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' /* optional */
    });
    //表单验证
    $("#userLoginFrm").validate({
      rules: {
        username: {
          required: true,
          minlength: 2,
          //异步验证 开始
          remote: {
            url: "/static/json/username.json",//发送请求的url地址
            type: "get",        //请求方式
            dataType: "json",
            data: {
              username: function () {
                return $("#username").val();
              }
            },
            dataFilter: function (data, type) {  //过滤返回结果
              if (data == "true") {
                return true;  //true代表用户名还未存在
              }else {
                return false; //false代表用户名已经存在
              }
            }
          }
          //异步验证 结束
        },
        password: {
          required: true,
          minlength: 5
        },
      },
      messages: {
        username: {
          required: "请输入用户名",
          minlength: "用户名必需由两个字母组成",
          remote: "输入的用户名已经存在"
        },
        password: {
          required: "请输入密码",
          minlength: "密码长度不能小于 5 个字母"
        },
      }
    });

  });
  $(document).ready(
          function () {
             if($.cookie("password")){
               $("#username").val($.cookie("username"));
               $("#password").val($.cookie("password"));
               $("#rememberme").prop("checked",true);
               // $("#rememberme").attr("checked",true);
               $('#rememberme').iCheck({
                 checkboxClass: 'icheckbox_square-blue',
                 radioClass: 'iradio_square-blue',
                 increaseArea: '20%' /* optional */
               });
             }
/*            $.get("/getuserinfo",function (data,status) {
                      if(status == "success"){
                        window.location.href="/static/index.html";
                      }
                    },"JSON"
            );*/
          }
  );
  $("#signin").click(
          function () {
             var username = $("#username").val();
             var password = $("#password").val();
             var salt = "";

            var flag = $("#userLoginFrm").valid();
            if(!flag){
              //没有通过验证
              return;
            }
/*

             if(username.trim() == ""){
                $("#errormsg").text("用户名不能为空!");
                return;
             }
            if(password.trim() == ""){
              $("#errormsg").text("密码不能为空!");
              return;
            }

*/

            if(password != $.cookie("password")) {
                $.ajaxSettings.async = false;
                $.get("/api/user/salt?username=" + username, function (data, status) {
                  if (status == "success") {
                    if(data.length > 10)
                       salt = data;
                  }
                });
                $.ajaxSettings.async = true;

                password = $.md5(password); //产生一个32位的字符串，checksum xxxx.file
                password = $.md5(password + salt);   //加盐
                password = $.md5(password + salt);   //加盐
                password = $.md5(password + salt);   //加盐  32bit
            }

            $.ajax({
              type: "POST",
              url: "/api/user/login",    //CORS
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({
                "username": username,
                "password": password
              }),
              dataType: "json",
              success: function (data) {
                var json = data;
                if(json.code == '200') {
                  if (json.data.success == 0) {
                    $("#errormsg").text("用户名或密码错误");
                  } else if (json.data.success == 1) {
                    if($("#rememberme").prop("checked")){
                      $.cookie("username", username, {expired: 7});
                      $.cookie("password", password, {expired: 7});
                      $.cookie("rememberme", true, {expired: 7});
                      $.cookie("sessionId", data.data.sessionId, {expired: 7});
                    }else{
                      $.cookie("username","",{expired:-1});
                      $.cookie("password","",{expired:-1});
                      $.cookie("rememberme",false,{expired:-1});
                    }
                    window.location.href = "/static/index.html";
                  }
                }else{
                   alert(data.msg);
                }
              },
              error: function (message) {
                $("#request-process-patent").html("提交数据失败！");
              }
            });
          }
  );
</script>
</body>
</html>
